package com.sanjiahantang.youjiarole.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiarole.base.BaseView;
import com.sanjiahantang.youjiarole.base.CallBack;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.config.SPConfig;
import com.sanjiahantang.youjiarole.config.StaticValue;
import com.sanjiahantang.youjiarole.module.common.beans.RoleStateBean;
import com.sanjiahantang.youjiarole.module.common.models.impl.UserModel;
import com.sanjiahantang.youjiarole.module.join.views.impl.CertificationActivity;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @desc:一些用到的验证.
 */

public class CheckUtil implements StaticValue{

    /*
     * 验证手机号 ,
     */
    public static boolean isMobileNO(String mobiles) {
        String regExp = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[0-9])|(18[0-9]))\\d{8}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(mobiles);
        boolean ismobi = m.find();
        return ismobi;
    }

    /**
     * 验证邮箱.
     */
    public static boolean isEmail(String strEmail) {
        String regExp = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(strEmail);
        return m.matches();
    }

    /**
     * 判断是否全是数字
     *
     * @param str
     * @return
     */
    public boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    /**
     * 是否全是中文
     *
     * @param str
     * @return
     */
    public static boolean isChinese(String str) {
        Pattern pattern = Pattern.compile("[^(\u4e00-\u9fa5)]");
        Matcher isChineseStr = pattern.matcher(str);
        if (!isChineseStr.find()) {
            return false;
        }

        return true;
    }

    /**
     * 姓名验证
     * @param str
     * @return
     */
    public static boolean isName(String str){
        Pattern pattern = Pattern.compile("[^(\u4e00-\u9fa5)]");
        Matcher isChineseStr = pattern.matcher(str);
        if (!isChineseStr.find()) {
            return false;
        }

        if (str.length() < 2 || str.length() > 4){
            return false;
        }
        return true;
    }


    /**
     *  验证角色
     * @param context
     * @param listener
     */
    public static void isRoleValid(final Context context, final BaseView.OnLoadListener listener){
        isRoleValid(context,false,listener);
    }

    /**
     * 验证角色
     * @param context
     * @param whenCancelStillGo
     * @param listener
     */
    public static void isRoleValid(final Context context,final boolean whenCancelStillGo,final BaseView.OnLoadListener listener) {
        String roleId = SPUtils.get(context, SPConfig.ROLE_ID, "").toString();
        if (!TextUtils.isEmpty(roleId)) {
            listener.onLoad(true);
        } else {
            Map<String,String> param = new HashMap<>();
            param.put(KEY_TOKEN, ConfigValue.DATA_KEY);
            param.put(KEY_PHONE,SPUtils.get(context,SPConfig.PHONE,"").toString());
            new UserModel().findUserIdState(context,param, new CallBack<RoleStateBean>() {
                @Override
                public void onSuccess(RoleStateBean response, final Context context, Object tag) {
                    if (response != null && response.getCode() == RESPONSE_CODE_SUC){
                        SPUtils.put(context,SPConfig.ROLE_ID,response.getRoleId());
                        if (response.getRoleState() == 1){
                            if (whenCancelStillGo){
                                listener.onLoad(null);
                            }
                            Utils.showDialog((Activity) context, "温馨提示", "您尚未认证加盟信息", "取消", "去认证", new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    Utils.dismissDialog();
                                    switch (view.getId()) {
                                        case com.sanjiahantang.uilibrary.R.id.txtDialogCancel:

                                            break;
                                        case com.sanjiahantang.uilibrary.R.id.txtDialogSure:
                                            ((Activity) context).startActivity(new Intent(context, CertificationActivity.class));
                                            break;
                                    }
                                }
                            });
                        }else if (response.getRoleState() == 0){
                            if (whenCancelStillGo){
                                listener.onLoad(null);
                            }
                            Utils.showDialog((Activity) context, "温馨提示", "加盟信息正在审核中，请耐心等待", "", "知道了", 1,new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    Utils.dismissDialog();
                                }
                            });
                        }else{
                            listener.onLoad(null);
                        }

                    }else{

                    }

                }

                @Override
                public void onFailure(String message) {

                }
            });

        }
    }

    public static void getCurrentRoleId(Context context, final BaseView.OnLoadListener listener){
        getCurrentRoleId(context,true,listener);
    }
    public static void getCurrentRoleId(Context context, final boolean flag, final BaseView.OnLoadListener listener){
        Map<String,String> param = new HashMap<>();
        param.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        param.put(KEY_PHONE,SPUtils.get(context,SPConfig.PHONE,"").toString());
        new UserModel().findUserIdState(context,param, new CallBack<RoleStateBean>() {
            @Override
            public void onSuccess(RoleStateBean response, final Context context, Object tag) {
                if (response != null && response.getCode() == RESPONSE_CODE_SUC){
                    SPUtils.put(context,SPConfig.ROLE_ID,response.getRoleId());
                    if (response.getRoleState() == 1){
                        listener.onLoad(null);
                    }else if (response.hasCheckingRole()){
                        if (flag){
                            Utils.showDialog((Activity) context, "温馨提示", "加盟信息正在审核中，请耐心等待", "", "知道了", 1,new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    Utils.dismissDialog();
                                }
                            });
                        }else {
                            listener.onLoad(null);
                        }
                    }else{
                        listener.onLoad(null);
                    }

                }else{
                    if (!flag){
                        listener.onLoad(null);
                    }
                }

            }

            @Override
            public void onFailure(String message) {
                if (!flag){
                    listener.onLoad(null);
                }
            }
        });
    }

    public static void checkTokenTimeOut(Context context, final BaseView.OnLoadListener listener){
        Map<String,String> param = new HashMap<>();
        param.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        param.put(KEY_PHONE,SPUtils.get(context,SPConfig.PHONE,"").toString());
        new UserModel().findUserIdState(context,param, new CallBack<RoleStateBean>() {
            @Override
            public void onSuccess(RoleStateBean response, final Context context, Object tag) {
                if (response != null && response.getCode() == RESPONSE_CODE_SUC){
                    listener.onLoad(null);
                }
            }

            @Override
            public void onFailure(String message) {

            }
        });
    }


    /**
     * 是否有表情
     * @param str
     * @return
     */
    public static boolean isEmoji(String str){
        Pattern pattern = Pattern.compile("[(\\ud83c\\udf00-\\ud83c\\udfff)|(\\ud83d\\udc00-\\ud83d\\ude4f)|(\\ud83d\\ude80-\\ud83d\\udeff)]");
        Matcher isEmojiStr = pattern.matcher(str);
        if (!isEmojiStr.find()){
            return false;
        }
        return true;
    }

}
